package com.victualler.business.purchase.service.impl;

import com.victualler.business.purchase.dto.PurchaseOrderInfoDto;
import com.victualler.business.purchase.entity.PurchaseItemDao;
import com.victualler.business.purchase.entity.PurchaseOrderDao;
import com.victualler.business.purchase.entity.PurchaseRelateDao;
import com.victualler.business.purchase.enums.CheckStateEnum;
import com.victualler.business.purchase.repository.PurchaseOrderQueryRepository;
import com.victualler.business.purchase.repository.PurchaseOrderRepository;
import com.victualler.business.purchase.service.PurchaseItemService;
import com.victualler.business.purchase.service.PurchaseOrderService;
import com.victualler.business.purchase.service.PurchaseRelateService;
import com.victualler.core.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 17:03 2018/4/5
 * @Discription:  Dict字典类型对应的服务层
 */
@Service
public class PurchaseOrderServiceImpl extends BaseServiceImpl<PurchaseOrderDao,PurchaseOrderRepository,PurchaseOrderQueryRepository> implements PurchaseOrderService {

    /**子信息*/
    @Autowired
    private PurchaseItemService purchaseItemService;
    /**关联订单信息*/
    @Autowired
    private PurchaseRelateService purchaseRelateService;

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 预订单id
     * @Function  根据条件入参prePurchaseRequestDto动态分页查询权限
     */
    public PurchaseOrderInfoDto findInfo(@PathVariable("id") Long id) {
        PurchaseOrderDao purchaseOrderDao = this.getById(id);
        List<PurchaseItemDao> purchaseItemDaoList = this.purchaseItemService.getByParentId(id);
        List<PurchaseRelateDao> purchaseRelateDaoList = this.purchaseRelateService.getByParentId(id);
        return new PurchaseOrderInfoDto(purchaseOrderDao,purchaseItemDaoList,purchaseRelateDaoList);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param purchaseOrderInfoDto 预订单实体大对象
     * @Function  保存预订单实体大对象
     */
    @Transactional
    public void saveInfo(PurchaseOrderInfoDto purchaseOrderInfoDto) {
        //如果是提交类型 则更新提交时间
        if (CheckStateEnum.UN_CHECK.getKey().equals(String.valueOf(purchaseOrderInfoDto.getPurchaseOrderDao().getCheckState()))) {
            purchaseOrderInfoDto.getPurchaseOrderDao().setCommitDate(new Date());
        }
        if(purchaseOrderInfoDto.getPurchaseOrderDao().getId() == null) {
            //生成订单号
            this.save(purchaseOrderInfoDto.getPurchaseOrderDao());
            purchaseOrderInfoDto.getPurchaseOrderDao().setOrderNo("PP" + purchaseOrderInfoDto.getPurchaseOrderDao().getId());
            //刷新字表数据
            for (PurchaseItemDao purchaseItemDao : purchaseOrderInfoDto.getPurchaseItemList()) {
                purchaseItemDao.setParentId(purchaseOrderInfoDto.getPurchaseOrderDao().getId());
            }
            this.purchaseItemService.saveAll(purchaseOrderInfoDto.getPurchaseItemList());
            //刷新字表数据
            for (PurchaseRelateDao purchaseRelateDao : purchaseOrderInfoDto.getPurchaseRelateDaoList()) {
                purchaseRelateDao.setParentId(purchaseOrderInfoDto.getPurchaseOrderDao().getId());
            }
            this.purchaseRelateService.saveAll(purchaseOrderInfoDto.getPurchaseRelateDaoList());
        } else {
            //更新
            this.save(purchaseOrderInfoDto.getPurchaseOrderDao());
            //更新明细信息
            this.purchaseItemService.updateList(purchaseOrderInfoDto.getPurchaseItemList(),purchaseOrderInfoDto.getPurchaseOrderDao().getId());
            this.purchaseRelateService.updateList(purchaseOrderInfoDto.getPurchaseRelateDaoList(),purchaseOrderInfoDto.getPurchaseOrderDao().getId());
        }
    }
}
